﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Infrastructures;
using Models;
using System.IO;

namespace SponsorshipWeb
{
    public partial class Step3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //var user = AuthUtil.GetAuthentication();
                //if (user == null)
                //    Response.Redirect("../login.html");

                //if (user.CompanyID == 0)
                //    Response.Redirect("guide.html");

                ////string id = "20000001"; //Request.QueryString["ID"];
                ////if (string.IsNullOrWhiteSpace(id))
                ////    Response.Redirect("index.html");

                //hfCompanyID.Value = user.CompanyID.ToString();

                //string mode = Request.QueryString["mode"];
                //if (mode == "edit")
                //{
                //    hfMode.Value = mode;
                //    BindUI(hfCompanyID.Value);
                //}
            }
        }

        private void BindUI(string id)
        {
            //ICompanyService service = ServiceFactory.CreateInstance<ICompanyService>();
            //var company = service.Get<Company>(hfCompanyID.Value);

            //IUtilService utilService = ServiceFactory.CreateInstance<IUtilService>();
            //var lst = utilService.ListFileSystem(company.CompanyNO);

            //txtName.Text = company.LicenseName;
            //txtNo.Text = company.LicenseNO;

            //imgLicense.Src = lst.FirstOrDefault(x => x.Fun == FileType.License).Path;
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            //string strError = string.Empty;

            //if (string.IsNullOrWhiteSpace(hfMode.Value))
            //{
            //    if (fileUpload.HasFile == false || String.IsNullOrEmpty(fileUpload.PostedFile.FileName))
            //    {
            //        strError = "ParseExcel:: No file found, please input a file.";
            //        return;
            //    }
            //}

            //ICompanyService service = ServiceFactory.CreateInstance<ICompanyService>();
            //var company = service.Get<Company>(hfCompanyID.Value);

            //company.LicenseName = txtName.Text;
            //company.LicenseNO = txtNo.Text;
            //service.Update(company, hfUserID.Value);

            //IList<FileSystem> lst = null;

            //if (hfMode.Value == "edit" && fileUpload.HasFile)
            //{
            //    IUtilService utilService = ServiceFactory.CreateInstance<IUtilService>();
            //    lst = utilService.ListFileSystem(Int32.Parse(hfCompanyID.Value));
            //}

            //SaveFile(fileUpload, FileType.License, lst);

            //Response.Redirect("info.html");
        }

        private void SaveFile(FileUpload fu, FileType type, IList<FileSystem> lst)
        {
            //if (!fu.HasFile)
            //    return;

            //string fileName = fu.FileName;
            //string fileExt = Path.GetExtension(fileName);
            //string strDir = SystemConfig.Get("UploadPath") + "Company\\" + hfCompanyID.Value + "\\";

            //if (!Directory.Exists(strDir))
            //    Directory.CreateDirectory(strDir);

            //string strSysName = type.ToString() + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
            //fu.SaveAs(strDir + strSysName);

            //FileSystem fs = null;

            //if (lst != null && lst.Count > 0)
            //    fs = lst.FirstOrDefault(x => x.Fun == type);
            //else
            //    fs = new FileSystem();

            //fs.Fun = type;
            //fs.Belong = Int32.Parse(hfCompanyID.Value);
            //fs.TypeName = APPRole.Company;
            //fs.NewName = strSysName;
            //fs.OldName = fileName;
            //fs.Path = SystemConfig.Get("UploadHttpPath") + "Company/" + hfCompanyID.Value + "/" + strSysName;
            //fs.Status = CommonStatus.Active;
            //fs.InstanceID = SystemConfig.Get("InstanceID");
            //fs.CreateUser = Int32.Parse(hfCompanyID.Value);

            //IUtilService service = FactoryCreator.Create<IUtilService>();

            //if (lst == null || lst.Count == 0)
            //    service.CreateFile(fs);
            //else
            //    service.UpdateFile(fs);
        }
    }
}